package com.kfm.jdbc.day0329;

import com.alibaba.druid.pool.DruidDataSource;
import com.kfm.jdbc.day0328.entity.Account;
import com.mysql.cj.jdbc.MysqlDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.List;
import java.util.Properties;

public class Demo01 {

    public static void main(String[] args) {
        Properties properties = new Properties();
        try {
            InputStream resourceAsStream = Demo01.class.getResourceAsStream("db.properties");
            properties.load(resourceAsStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        DruidDataSource dataSource = new DruidDataSource();

        dataSource.setDriverClassName(properties.getProperty("jdbc.driver"));
        dataSource.setUrl(properties.getProperty("jdbc.url"));
        dataSource.setUsername(properties.getProperty("jdbc.user"));
        dataSource.setPassword(properties.getProperty("jdbc.password"));

        // 连接池
        dataSource.setMaxActive(20);
        dataSource.setMinIdle(5);
        dataSource.setMaxWait(60000);
        dataSource.setValidationQuery("SELECT 1");

        QueryRunner queryRunner = new QueryRunner(dataSource);
        String sql = "select * from account";
        try {
            List<Account> account = queryRunner.query(sql, new BeanListHandler<>(Account.class));
            System.out.println(account);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
}
